
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.controller;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import module.GameTurn;
import module.ManagePlayer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import wargame.dao.NotificationDAO;
import wargame.entity.Notification;
import wargame.entity.Player;

/**
 *
 * @author stanley.2007
 */
@Controller
@RequestMapping("/player")
public class PlayerController {

    // accessed through http://localhost:8080/wargaming/servlet/player/dashboard
    @RequestMapping(value = "/dashboard", method = RequestMethod.GET)
    public ModelAndView viewDashboard(Model model, HttpServletRequest request,
            @RequestParam(value = "announcement", required = false) String announcement) {

        ModelAndView mv = new ModelAndView();

        Player player = ManagePlayer.getThisPlayer(request);

        if (player == null) {
            mv.setViewName("register");
            return mv;
        }

        ArrayList<Notification> notification_list = NotificationDAO.retrievePlayerNotification(player.getPlayer_id());

        for(Notification notification:notification_list){
            if(notification.getUnread() == 1){
                NotificationDAO.updateToRead(notification.getNotification_id());
            }
        }

        mv.setViewName("index");
        mv.addObject("player", player);
        mv.addObject("notification_list", notification_list);
        mv.addObject("turn", GameTurn.getTurnInfo());
        if (announcement != null) {
            mv.addObject("announcement", announcement);
        }

        return mv;
    }
}
